;(function($, window, document, undefined){
    var PLUGIN_NAME = 'Menu';
    $[PLUGIN_NAME] = $[PLUGIN_NAME] || {};
    // {element:$node, data:array, onNav, singleOpen:false }
    $[PLUGIN_NAME] = function(options){

        // class flag : '__menu__' 

        this.defaultOptions = {
            onNav: function(/* level, parent, data*/){},
            singleOpen: false,
            highlight: false
        };


        // functions
        var generator = function(callback){

            this.getMenu = function(data){
                var self = this;
                var $root = $('<ul>').addClass('__menu__');
                $.each(data, function(index1, value1){
                    var $li = $('<li>');
                    $li.append(self.getLinkItem(1, undefined, value1));
                    if(value1.children && value1.children.length > 0){
                        $li.append(self.getLinkItems(2, value1, value1.children));
                    }
                    $root.append($li);
                });
                return $root;
            };

            this.getLinkItems = function(level, parent, data){
                var self = this;
                var $ul = $('<ul>');
                $.each(data, function(index, value){
                    var $li = $('<li>');
                    $li.append(self.getLinkItem(level, parent, value));
                    $ul.append($li);
                });
                return $ul;
            };

            // <div><img alt=" " /><span>系统设置2</span></div> images/menuItem/532.png
            this.getLinkItem = function(level, parent, data){
                var imgSrc = (level == 1 ? 'images/menuItem/448.png' : 'images/menuItem/532.png' );
                if(data.img){
                    imgSrc = data.img;
                }
                var $img = $('<img>').attr('src',imgSrc);
                var $span = $('<span>').html(data.title);
                
                var $div = $('<div>').append($img).append($span);
                data.$menuObj = $div;

                $div.click(function(){
                    if($(this).next().is(":visible")){
                        $(this).next().slideUp();
                    }else{
                        $(this).next().slideDown();
                    }
                    if(data.event != false){
                        callback(level, parent, data);
                    }
                    
                });

                return $div;
            }

            return this;

        };

        this.init = function(){
            var self = this;
            this.options = $.extend({}, this.defaultOptions, options);

            var callback = this.options.onNav;
            if(this.options.highlight){
                callback = function(level, parent, data){
                    self.highlight(data.$menuObj)
                    self.options.onNav(level, parent, data);
                }
            }
            var _gen = new generator(callback);

            var menuHtml = _gen.getMenu(this.options.data);
            this.options.element.html(menuHtml);

            // console.log("menu init this.options.data", this.options.data);
            // console.log("menu init this", this);
        };

        this.openAll = function(){
            $(".__menu__>li>ul").slideDown();
        },

        this.closeAll = function(){
            $(".__menu__>li>ul").slideUp();
        };

        this.highlight = function($menuObj){
            $(".__menu__>li>ul>li div ").removeClass("__menu__highlight");
            $menuObj.addClass("__menu__highlight");
        };

        // nav by ID
        this.nav = function(menuDataID){
            var self = this;
            $.each(this.options.data,function(index1, value1){
                if(value1.id == menuDataID){
                    if(value1.event != false){
                        self.options.onNav(1, undefined, value1);
                        if(self.options.highlight){
                            self.highlight(value1.$menuObj);
                        }
                    }
                    return;
                }
                if(value1.children && value1.children.length > 0){
                    $.each(value1.children, function(index2, value2){
                        if(value2.id == menuDataID){
                            if(value2.event != false){
                                self.options.onNav(2, value1, value2);
                                if(self.options.highlight){
                                    self.highlight(value2.$menuObj);
                                }
                            }
                            return;
                        }
                    });
                }
            });
        };

        this.init();
        



        return this;
        
    

    };




    // --------result html------------
    // <ul class="menuRoot">
    //     <li>
    //         <div><img src="images/menuItem/532.png"/><span>系统设置</span></div>
    //         <ul>
    //             <li><div><img src="images/menuItem/532.png"/><span>模块管理</span></div></li>
    //             <li><div><img src="images/menuItem/532.png"/><span>角色管理</span></div></li>
    //             <li><div><img src="images/menuItem/532.png"/><span>用户管理</span></div></li>
    //         </ul>
    //     </li>
    //     <li>
    //         <div><img alt=" " /><span>系统设置2</span></div>
    //         <ul>
    //             <li><div>模块管理2</div></li>
    //             <li><div>角色管理2</div></li>
    //             <li><div>用户管理2</div></li>
    //         </ul>
    //     </li>
    // </ul>


})(jQuery, window, document);